/**
 * 내용 1. 송수신 내용 표시
 * 		2. 메시지를 전송할 클라이언트 선택(귓속말)
 */

package jong;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;

import javax.swing.*;
import javax.swing.border.*;

public class MultiChatClient extends JFrame implements Runnable {
	InetAddress ia = null;
	SocketData sd = null;
	Socket socket = null;
	MultiChatClientThread mcct = null;
	
	StringBuilder data = new StringBuilder();
	boolean tFlag = false;
	int port = 4444;
	
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	ObjectOutputStream oos = null;
	
	private JPanel contentPane;
	private JPanel panel;
	private JButton btnDisconnect;
	private JButton btnConnect;
	private JSeparator separator;
	private JPanel panel_1;
	private JButton btnSend;
	JTextField message;
	private JComboBox comboBox;
	private JSeparator separator_1;
	private JPanel panel_2;
	private JScrollPane scrollPane;
	private JList list;
	private JLabel label;
	private JPanel panel_3;
	private JSeparator separator_2;
	private JScrollPane scrollPane_1;
	private JTextPane result;
	private JLabel label_1;
	private JButton btnClose;
	private JPanel panel_4;
	JTextField IP;
	JTextField ID;
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MultiChatClient frame = new MultiChatClient();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MultiChatClient() {
		setTitle("Multi Client");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 500, 400);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.add(getPanel(), BorderLayout.NORTH);
		contentPane.add(getPanel_1(), BorderLayout.SOUTH);
		contentPane.add(getPanel_2(), BorderLayout.WEST);
		contentPane.add(getPanel_3(), BorderLayout.CENTER);
		try{
			ia = InetAddress.getLocalHost();
			setTitle(ia.getHostAddress());
			IP.setText(ia.getHostAddress());
		}catch(Exception ex){
			ex.getMessage();
		}
		btnDisconnect.setEnabled(false);
	}

	@Override
	public void run() {
		try{
			socket = new Socket(IP.getText(), port);
			mcct = new MultiChatClientThread(this, socket);
			mcct.start();
		}catch(Exception ex){
			ex.getMessage();
		}
		
		btnConnect.setEnabled(true);
		btnDisconnect.setEnabled(false);
	}
	public void serverConnect(){
		try{
			dataSet("<hr/><font color='red'>서버에 접속했습니다.</font><br/>" +
					"<font color='blue'>서버 접속 시간 : " + sdf.format(new Date()) + "</font><br/><hr/>");
			
			Thread t = new Thread(this);
			t.start();

			btnConnect.setEnabled(false);
			btnDisconnect.setEnabled(true);
		}catch(Exception ex){
			ex.getMessage();
		}
	}
	public void serverDisconnect(){
		try{

		}catch(Exception ex){
			ex.getMessage();
		}
	}
	public void sendMsg(String s){
		try{
			SocketData sd = new SocketData("login", ID.getText(), message.getText(), null);
			sd.setMessage(s);
			mcct.send.writeObject(sd);
			dataSet(sd);
			message.setText("");
		}catch(Exception ex){
			ex.getMessage();
		}
	}
	public void dataSet(SocketData sd){
		data.append("[" + sd.getUser() + "]" + sd.getMessage() + "<br/>");
		result.setText(data.toString());
	}
	
	public void dataSet(String str){
		data.append(str + "<br/>");
		result.setText(data.toString());
	}
	
	private JButton getBtnConnect() {
		if (btnConnect == null) {
			btnConnect = new JButton("연결");
			btnConnect.setMargin(new Insets(20, 20, 20, 20));
			btnConnect.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					serverConnect();
				}
			});
			btnConnect.setPreferredSize(new Dimension(60, 30));
		}
		return btnConnect;
	}
	private JButton getBtnDisconnect() {
		if (btnDisconnect == null) {
			btnDisconnect = new JButton("종료");
			btnDisconnect.setPreferredSize(new Dimension(100, 23));
			btnDisconnect.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					serverDisconnect();
				}
			});
		}
		return btnDisconnect;
	}
	private JButton getBtnSend() {
		if (btnSend == null) {
			btnSend = new JButton("전송");
			btnSend.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					sendMsg(message.getText());
				}
			});
			btnSend.setPreferredSize(new Dimension(100, 30));
		}
		return btnSend;
	}
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getBtnDisconnect(), BorderLayout.EAST);
			panel.add(getBtnConnect(), BorderLayout.CENTER);
			panel.add(getSeparator(), BorderLayout.SOUTH);
			panel.add(getPanel_4(), BorderLayout.WEST);
		}
		return panel;
	}
	private JSeparator getSeparator() {
		if (separator == null) {
			separator = new JSeparator();
			separator.setPreferredSize(new Dimension(0, 10));
			separator.setForeground(SystemColor.menu);
			separator.setEnabled(false);
		}
		return separator;
	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setLayout(new BorderLayout(0, 0));
			panel_1.add(getBtnSend(), BorderLayout.EAST);
			panel_1.add(getMessage(), BorderLayout.CENTER);
			panel_1.add(getComboBox(), BorderLayout.WEST);
			panel_1.add(getSeparator_1(), BorderLayout.NORTH);
		}
		return panel_1;
	}
	public JTextField getMessage() {
		if (message == null) {
			message = new JTextField();
			message.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					if(e.getKeyCode() == KeyEvent.VK_ENTER){
						sendMsg(message.getText());

					}
				}
			});
			message.setColumns(25);
		}
		return message;
	}
	private JComboBox getComboBox() {
		if (comboBox == null) {
			comboBox = new JComboBox();
			comboBox.setPreferredSize(new Dimension(70, 21));
			comboBox.setName("이모티콘");
		}
		return comboBox;
	}
	private JSeparator getSeparator_1() {
		if (separator_1 == null) {
			separator_1 = new JSeparator();
			separator_1.setPreferredSize(new Dimension(0, 10));
			separator_1.setForeground(Color.WHITE);
		}
		return separator_1;
	}
	private JPanel getPanel_2() {
		if (panel_2 == null) {
			panel_2 = new JPanel();
			panel_2.setLayout(new BorderLayout(0, 0));
			panel_2.add(getScrollPane(), BorderLayout.CENTER);
			panel_2.add(getBtnClose(), BorderLayout.SOUTH);
		}
		return panel_2;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setPreferredSize(new Dimension(170, 200));
			scrollPane.setColumnHeaderView(getLabel());
			scrollPane.setViewportView(getList());
		}
		return scrollPane;
	}
	private JList getList() {
		if (list == null) {
			list = new JList();
			list.setPreferredSize(new Dimension(160, 0));
		}
		return list;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("접속자 목록");
			label.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
			label.setPreferredSize(new Dimension(64, 20));
			label.setOpaque(true);
			label.setHorizontalAlignment(SwingConstants.CENTER);
			label.setBackground(Color.LIGHT_GRAY);
		}
		return label;
	}
	private JPanel getPanel_3() {
		if (panel_3 == null) {
			panel_3 = new JPanel();
			panel_3.setLayout(new BorderLayout(0, 0));
			panel_3.add(getScrollPane_1(), BorderLayout.CENTER);
			panel_3.add(getSeparator_2(), BorderLayout.WEST);
		}
		return panel_3;
	}
	private JSeparator getSeparator_2() {
		if (separator_2 == null) {
			separator_2 = new JSeparator();
			separator_2.setPreferredSize(new Dimension(10, 2));
			separator_2.setForeground(Color.LIGHT_GRAY);
			separator_2.setEnabled(false);
		}
		return separator_2;
	}
	private JScrollPane getScrollPane_1() {
		if (scrollPane_1 == null) {
			scrollPane_1 = new JScrollPane();
			scrollPane_1.setViewportView(getResult());
			scrollPane_1.setColumnHeaderView(getLabel_1());
		}
		return scrollPane_1;
	}
	private JTextPane getResult() {
		if (result == null) {
			result = new JTextPane();
			result.setContentType("text/html");
		}
		return result;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("내용");
			label_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
			label_1.setPreferredSize(new Dimension(64, 20));
			label_1.setOpaque(true);
			label_1.setHorizontalAlignment(SwingConstants.CENTER);
			label_1.setBackground(Color.LIGHT_GRAY);
		}
		return label_1;
	}
	private JButton getBtnClose() {
		if (btnClose == null) {
			btnClose = new JButton("귓속말 해제");
		}
		return btnClose;
	}
	private JPanel getPanel_4() {
		if (panel_4 == null) {
			panel_4 = new JPanel();
			panel_4.setLayout(new GridLayout(0, 2, 0, 0));
			panel_4.add(getLblNewLabel());
			panel_4.add(getLblNewLabel_1());
			panel_4.add(getIP());
			panel_4.add(getID());
		}
		return panel_4;
	}
	public JTextField getIP() {
		if (IP == null) {
			IP = new JTextField();
			IP.setColumns(10);
		}
		return IP;
	}
	public JTextField getID() {
		if (ID == null) {
			ID = new JTextField();
			ID.setColumns(10);
		}
		return ID;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("IP");
			lblNewLabel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
			lblNewLabel.setBackground(Color.LIGHT_GRAY);
			lblNewLabel.setOpaque(true);
			lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblNewLabel;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("ID");
			lblNewLabel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
			lblNewLabel_1.setOpaque(true);
			lblNewLabel_1.setBackground(Color.LIGHT_GRAY);
			lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblNewLabel_1;
	}
}
